package com.zyd.shiro.business.controller.pak;

import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.Purchase;
import com.zyd.shiro.business.service.PakAssetsClassService;
import com.zyd.shiro.business.service.PakAssetsService;
import com.zyd.shiro.business.service.PakPurchaseLineService;
import com.zyd.shiro.business.service.PakPurchaseService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.framework.object.PageResult;
import com.zyd.shiro.framework.object.ResponseVO;
import com.zyd.shiro.persistence.beans.PakPurchaseLine;
import com.zyd.shiro.util.ResultUtil;
import com.zyd.shiro.utils.ExcelTool;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.zyd.shiro.persistence.beans.PakPurchase;
import io.swagger.annotations.ApiOperation;

import java.util.List;
import java.io.File;
import com.zyd.shiro.utils.FileUtils;
import com.zyd.shiro.utils.ExcelUtil;
import java.util.Date;
import java.util.Map;


@RestController
@RequestMapping("/purchase")
@Api(description = "资产采购")
public class PakPurchaseController {
    @Autowired
    private PakPurchaseService purchaseService;

    @Autowired
    private PakPurchaseLineService purchaseLineService;

    @Autowired
    private PakAssetsService assetsService;

    @Autowired
    private PakAssetsClassService assetsClassService;

    @PostMapping
    @ApiOperation(value="添加", produces="application/json")
    public ResponseVO insert(Purchase purchase) {
        purchase.getPakPurchase().setUnicode("pu"+System.currentTimeMillis());
        purchaseService.insert(purchase);
        return ResultUtil.success("添加成功！",purchase);
    }

    @PutMapping
    @ApiOperation(value="修改", produces="application/json")
    public ResponseVO update(Purchase purchase) {
        purchaseService.updateSelective(purchase);
        return ResultUtil.success("修改成功！",null);
    }

    @DeleteMapping(value = "/{id}")
    @ApiOperation(value="主键删除", produces="application/json")
    public ResponseVO delete(@PathVariable(name="id") Long id){
        purchaseService.removeByPrimaryKey(id);
        return ResultUtil.success("删除成功！",null);
    }

    @GetMapping
    @ApiOperation(value="分页查询", produces="application/json")
    public PageResult page(BaseConditionVO vo) {
        PageInfo<Purchase> pageInfo = purchaseService.findPageBreakByCondition(vo);
        return ResultUtil.tablePage(pageInfo);
    }

    @GetMapping(value = "/all")
    @ApiOperation(value="查询所有", produces="application/json")
    public ResponseVO list() {
        List<Purchase> purchases = purchaseService.listAll();
        return ResultUtil.success("查询成功！",purchases);
    }
    @GetMapping(value = "/template")
    @ApiOperation(value="导入模板", produces="application/json")
    public ResponseVO template() {
        return ResultUtil.success("查询成功！","/upload/purchase.xlsx");
    }

    @PostMapping(value = "/import")
    @ApiOperation(value="批量导入", produces="application/json")
    public ResponseVO folderImport(MultipartFile file) throws Exception {
        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.indexOf("."), fileName.length());
        if (suffix.equals(".xlsx") || suffix.equals(".xls")) {
            File orFile = new File(FileUtils.createFilePath(fileName));
            file.transferTo(orFile);
            List<Map<String, String>> list = ExcelTool.listMainData(orFile,PakPurchase.class);
            if(list.size()==0){
                return ResultUtil.error("主体信息缺失!");
            }
            Purchase purchase = purchaseService.mapConvObject(list.get(0));

            ExcelUtil<PakPurchaseLine> excelUtil = new ExcelUtil<>();
            List<PakPurchaseLine> pakPurchaseLines = excelUtil.listFromData(orFile,PakPurchaseLine.class);
            for (PakPurchaseLine pakPurchaseLine : pakPurchaseLines) {
                pakPurchaseLine.setPurchase(purchase.getId());
                pakPurchaseLine.setCategories(assetsClassService.findByName(pakPurchaseLine.getCategoriesName()));
                pakPurchaseLine.setCreateTime(new Date());
                pakPurchaseLine.setUpdateTime(new Date());
                purchaseLineService.increase(pakPurchaseLine);
            }
            return ResultUtil.success("上传成功！");
        }
        return ResultUtil.error("请输入excel文件!");
    }

    @PostMapping(value = "/warehousing/{id}")
    @ApiOperation(value="入库", produces="application/json")
    public ResponseVO Warehousing(@PathVariable(name="id") Long id) {
        Purchase purchase = purchaseService.getByPrimaryKey(id);
        PakPurchase pakPurchase = purchase.getPakPurchase();
        if(pakPurchase.getAppstatus()!=1){
            return ResultUtil.error("未通过审批！");
        }
        if(pakPurchase.getWarestatus()==1){
            return ResultUtil.error("已入库！");
        }
        List<PakPurchaseLine> purchaseLines = purchaseLineService.findByPurchase(id);
        for(PakPurchaseLine purchaseLine:purchaseLines){
            assetsService.increase(purchaseLine,pakPurchase);
        }
        pakPurchase.setWarestatus(1);
        purchaseService.updateSelective(purchase);
        return ResultUtil.success("入库成功！",purchase);
    }

    @PutMapping(value = "approve/{id}")
    @ApiOperation(value="审批", produces="application/json")
    public ResponseVO approve(@PathVariable(name="id") Long id,int status) {
        Purchase purchase = purchaseService.getByPrimaryKey(id);
        purchase.getPakPurchase().setAppstatus(status);
        purchaseService.updateSelective(purchase);
        return ResultUtil.success("审批成功！",null);
    }
}